package com.retail.marketing.server.service.impl;

import com.retail.common.core.domain.Result;
import com.retail.marketing.common.domain.TCoupon;
import com.retail.marketing.common.domain.TCouponType;
import com.retail.marketing.common.emu.CouponEnum;
import com.retail.marketing.common.req.ComputeReq;
import com.retail.marketing.server.mapper.TCouponRepertoryMapper;
import com.retail.marketing.server.mapper.TCouponTypeMapper;
import com.retail.marketing.server.strategy.CouponStrategy;
import com.retail.marketing.server.service.ITUserRepertoryService;
import com.retail.marketing.server.strategy.CouponStrategyContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ITUserRepertoryServiceImpl implements ITUserRepertoryService {
    @Autowired
    private TCouponRepertoryMapper tCouponRepertoryMapper;
    @Autowired
    private CouponStrategyContext couponStrategyContext;
    @Autowired
    private TCouponTypeMapper tCouponTypeMapper;
    @Override
    public List<TCoupon> selectTCount(Integer userId)
    {
        return tCouponRepertoryMapper.selectTCoupon(userId);
    }

    @Override
    public Result computePrice(ComputeReq req) {
        //根据优惠类型id查找到类型表里的数据
        TCouponType tCouponType = tCouponTypeMapper.selectTCouponTypeByCouponTypeId (req.getCouponTypeId ().longValue ());
        //查找出类型中的枚举值，通过枚举值转换为枚举对象获取引用上下文的接口对象
        CouponStrategy couponStrategy = couponStrategyContext.getType (Enum.valueOf (CouponEnum.class, tCouponType.getEnumValue ()));
        //获取的接口对象执行方法
        BigDecimal bigDecimal = couponStrategy.doActive (req);
        if (bigDecimal.compareTo (req.getOrderPrice ())==0){
            return Result.error (req.getOrderPrice (),"价格不满足优惠条件");
        }
        return Result.success (bigDecimal,"优惠后的价格为"+bigDecimal);
    }
}
